package com.dong.es.entity.searchparam.query.script;

import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
import com.dong.es.entity.EsBaseDTO;
import com.dong.es.entity.searchparam.query.EsQueryParam;
import org.apache.commons.lang3.StringUtils;

/**
 * @description: Es 脚本 相关参数
 * @author: 陈瑞东
 * @create: 2022-06-23 11:47
 */
public class EsScriptQueryParam extends EsQueryParam {
    private static final long serialVersionUID = -5138183067180098040L;

    private String inline;
    private String lang;

    public EsScriptQueryParam(String inline, String lang) {
        this.inline = inline;
        this.lang = lang;
    }

    @Override
    protected boolean valid(EsBaseDTO esBaseDTO) {
        return StringUtils.isNotBlank(inline) && StringUtils.isNotBlank(lang);
    }

    @Override
    public void loadTo(BoolQuery.Builder target) {
        target.filter(
            filter -> filter.script(
                script -> script.script(
                    sc -> sc.inline(
                            e -> e.source(inline)
                                    .lang(lang)
                    )
                )
            )
        );
    }
}
